package as.action;

import java.util.List;

import org.apache.struts2.ServletActionContext;

import as.dao.ClassHoursDAO;
import as.entity.ClassHours;
import as.entity.Register;
import as.service.ClassHoursService;
import as.service.CourseService;

public class ClassHoursAction extends BaseAction {
	private long courseId;
	private List<Register> list;
	private ClassHoursService classHoursService;
	private String url;
	private CourseService courseService;

	public String statistic() {
		if (courseId < 1) {
			url = ServletActionContext.getRequest().getRequestURL().toString()
					+ "?";
			return "input";
		}
		list = classHoursService.statistic(courseId);
		int totalClassHour = courseService.findCourseById(courseId)
				.getClassHour();
		for (Register r : list) {
			if (r.getClassHours() == null||r.getClassHours().getEarnClassHour()==null) {
				ClassHours classHours = new ClassHours();
				classHours.setEarnClassHour(0f);
				classHours.setUnEarnClassHour(new Float(totalClassHour));
				
				r.setClassHours(classHours);
			}else if (r.getClassHours().getEarnClassHour() > totalClassHour) {
				r.getClassHours().setEarnClassHour(new Float(totalClassHour));
				r.getClassHours().setUnEarnClassHour(0f);
			}else{
				r.getClassHours().setUnEarnClassHour(new Float(totalClassHour-r.getClassHours().getEarnClassHour()));
			}

		}
		return SUCCESS;
	}

	public long getCourseId() {
		return courseId;
	}

	public void setCourseId(long courseId) {
		this.courseId = courseId;
	}

	public List<Register> getList() {
		return list;
	}

	public void setList(List<Register> list) {
		this.list = list;
	}

	public void setClassHoursService(ClassHoursService classHoursService) {

		this.classHoursService = classHoursService;
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public void setCourseService(CourseService courseService) {
		this.courseService = courseService;
	}

}
